using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Voodoo.Pattern;

namespace Voodoo.GameOps
{
	public class InventoryManager : MonoBehaviour
	{
		private const string TOTAL_PURCHASED_SAWS = "TOTALAMOUNTOFPURCHASEDSAWS";

		public static InventoryManager Instance;

		public List<Item> AllItems = new List<Item>();

		public Action OnSkinChanged;

		public Action OnItemPurchased;

		private void Awake()
		{
			if (Instance != null)
			{
				Debug.LogError("InventoryManger - Cant have more than one Instance in a scene.");
			}
			if (AllItems == null || AllItems.Where((Item u) => u != null).ToList().Count == 0)
			{
				Debug.LogError("InventoryManger - List of items cant be empty.");
			}
			AllItems = AllItems.Where((Item u) => u != null).ToList();
			Instance = this;
		}

		internal void ResetToDefault(ItemType _type)
		{
			Item collectible = GetDefault(_type);
			SelectItem(collectible);
		}

		public Item GetRandomItem()
		{
			List<Item> items = AllItems.Where((Item u) => !IsUnlocked(u)).ToList();
			return _GetRandomItem(items);
		}

		public Item GetRandomItemOfType(ItemType _type)
		{
			List<Item> items = AllItems.Where((Item u) => u.ItemType == _type && !IsUnlocked(u)).ToList();
			return _GetRandomItem(items);
		}

		public Item _GetRandomItem(List<Item> _items)
		{
			if (_items.Count == 0)
			{
				return null;
			}
			return _items[UnityEngine.Random.Range(0, _items.Count)];
		}

		public List<Item> GetAllOfType(ItemType _type)
		{
			List<Item> list = (from u in AllItems
							   where u.ItemType == _type
							   orderby u.Order
							   select u).ToList();
			if (list.Count == 0)
			{
				return null;
			}
			return list;
		}

		public void UnlockItem(Item item)
		{
			NintendoPlayerPrefs.SetInt("Unlocked_" + item.ID, GetInventoryCount(item) + 1);
			if (item.ItemType == ItemType.Skin && !item.isPartOfDLC && !item.UnlockedByDefault)
			{
				int num = NintendoPlayerPrefs.GetInt("TOTALAMOUNTOFPURCHASEDSAWS", 0) + 1;
				NintendoPlayerPrefs.SetInt("TOTALAMOUNTOFPURCHASEDSAWS", num);
			}
			int num2 = item.ID switch
			{
				"TREADMILL" => 13,
				"TANK" => 14,
				"MULTIPLIER" => 15,
				"DISCOUNT" => 16,
				"SERVO" => 17,
				_ => -1,
			};

			OnItemPurchased?.Invoke();
		}

		public int GetInventoryCount(Item item)
		{
			if (!NintendoPlayerPrefs.HasKey("Unlocked_" + item.ID))
			{
				NintendoPlayerPrefs.SetInt("Unlocked_" + item.ID, 0);
			}
			return NintendoPlayerPrefs.GetInt("Unlocked_" + item.ID, 0);
		}

		public bool IsUnlocked(Item item)
		{
			if (!item.UnlockedByDefault)
			{
				return NintendoPlayerPrefs.GetInt("Unlocked_" + item.ID, 0) >= 1;
			}
			return true;
		}

		internal Item CurrentlySelected(ItemType category)
		{
			string CurrentlySelectedID = NintendoPlayerPrefs.GetString("Equiped_" + category, "Default");
			Item item = AllItems.Where((Item u) => u.ID == CurrentlySelectedID && u.ItemType == category).FirstOrDefault();
			if (item == null)
			{
				Item item2 = (from u in GetAllOfType(category)
							  where u.UnlockedByDefault
							  select u).FirstOrDefault();
				if (item2 != null)
				{
					SelectItem(item2);
					item = item2;
				}
			}
			return item;
		}

		internal int GetPrice(Item item)
		{
			return item.GetPrice(GetInventoryCount(item));
		}

		internal void SelectItem(Item collectible)
		{
			NintendoPlayerPrefs.SetString("Equiped_" + collectible.ItemType, collectible.ID);
			OnSkinChanged?.Invoke();
		}

		internal Item GetDefault(ItemType _type)
		{
			return AllItems.Where((Item u) => u.UnlockedByDefault && u.ItemType == _type).FirstOrDefault();
		}

		internal Item NextItemToUnlock()
		{
			return _NextUnlockItem(AllLockedItems());
		}

		internal Item NextItemToUnlock(ItemType _type)
		{
			return _NextUnlockItem(AllItemsAvailableOfType(_type));
		}

		private Item _NextUnlockItem(List<Item> _items)
		{
			int num = int.MaxValue;
			Item result = null;
			foreach (Item _item in _items)
			{
				int price = _item.Price;
				if (num > price)
				{
					num = price;
					result = _item;
				}
			}
			return result;
		}

		internal List<Item> AllItemsAvailableOfType(ItemType _type)
		{
			return AllItems.Where((Item u) => u.ItemType == _type && !IsUnlocked(u)).ToList();
		}

		internal List<Item> AllIUnlockedItems()
		{
			return AllItems.Where((Item u) => IsUnlocked(u)).ToList();
		}

		internal List<Item> AllLockedItems()
		{
			return AllItems.Where((Item u) => !IsUnlocked(u)).ToList();
		}

		internal bool ItemsAvailableToUnlock()
		{
			return AllItems.Where((Item u) => !IsUnlocked(u)).ToList().Count >= 1;
		}

		internal bool ItemsAvailable(ItemType _type)
		{
			return AllItems.Where((Item u) => u.ItemType == _type && !IsUnlocked(u)).ToList().Count >= 1;
		}

		internal Item GetItem(string ID)
		{
			return AllItems.Where((Item u) => u.ID.Equals(ID)).FirstOrDefault();
		}

		internal bool HasInventory(Item item)
		{
			if (!item.UnlockedByDefault)
			{
				return item.Max > GetInventoryCount(item);
			}
			return false;
		}

		public int RvCountToUnlockItem(Item item)
		{
			return item.RvPrice - NintendoPlayerPrefs.GetInt(item.ID + "Rv count to unlock", 0);
		}

		public void RvForUnlockItemShown(Item item)
		{
			NintendoPlayerPrefs.SetInt(item.ID + "Rv count to unlock", NintendoPlayerPrefs.GetInt(item.ID + "Rv count to unlock", 0) + 1);
		}

		public float CurrentBalance()
		{
			return NintendoPlayerPrefs.GetFloat("Money", 0f);
		}

		public float CurrentPrestigePointsBalance()
		{
			return NintendoPlayerPrefs.GetFloat("PrestigePoints", 0f);
		}

		public int CurrentGemsBalance()
		{
			return NintendoPlayerPrefs.GetInt("Gems", 0);
		}

		public void SpendMoney(float amount)
		{
			NintendoPlayerPrefs.SetFloat("Money", Mathf.Max(0f, CurrentBalance() - amount));
			SingletonMB<Money>.Instance.UpdateValue();
		}

		public void AddMoney(float amount)
		{
			NintendoPlayerPrefs.SetFloat("Money", CurrentBalance() + amount);
			SingletonMB<Money>.Instance.UpdateValue();
		}

		public void SpendPrestigePoints(float amount)
		{
			Debug.Log("SPENDING PRESTIGE POINTS " + CurrentPrestigePointsBalance() + " AMOUNT " + amount);
			NintendoPlayerPrefs.SetFloat("PrestigePoints", CurrentPrestigePointsBalance() - amount);
			SingletonMB<PrestigePointsCounter>.Instance.UpdateValue();
		}

		public void AddPrestigePoints(float amount)
		{
			NintendoPlayerPrefs.SetFloat("PrestigePoints", CurrentPrestigePointsBalance() + amount);
			SingletonMB<PrestigePointsCounter>.Instance.UpdateValue();
		}

		public void SpendGems(int amount)
		{
			NintendoPlayerPrefs.SetInt("Gems", CurrentGemsBalance() - amount);
			SingletonMB<GemsCounter>.Instance.UpdateValue();
		}

		public void AddGems(int amount)
		{
			NintendoPlayerPrefs.SetInt("Gems", CurrentGemsBalance() + amount);
			SingletonMB<GemsCounter>.Instance.UpdateValue();
		}
	}
}
